export default {

    namespace: 'example',

    state: {
        //datagrid数据
        dataSource: [],
        //数据列表总数
        totalItems: 0,
        //当前页
        page: 1,
        //每页数据行数
        rows: 10,
        //选择中的index
        selectedRowKeys: [],
        //是否展示modal框
        isShowModal: false,
        //当前modal弹框的类型(用于展示)
        modalType: '',
        modalData:{
            index: '',
            name: '',
            age: '',
            address: '',
            trainingClass: []
        },
        requiredFieldValidator: false
    },

    subscriptions: {
        setup({ dispatch, history }) {
        },
    },

    effects: {
        *save({payload},{select,call,put}){
            let {modalData,dataSource} = yield select(({example})=>example);
            let {name,age,address,trainingClass=[]} = modalData;
            if(name == '' || age == '' || address == ''){
                return false;
            }
            let d = [];
            if(modalData.index !== ''){
                d = dataSource.map((item,index)=>{
                    if(index+1 == modalData.index){
                        return {
                            ...item,
                            trainingClass: modalData.trainingClass.map((item,index)=>{
                                return {...item};
                            })
                        }
                    }
                    return item;
                })
            }else{
                d = [...dataSource]
                d.push({
                    key: d.length,
                    index: d.length+1,
                    name: modalData.name,
                    age: modalData.age,
                    address: modalData.address,
                    trainingClass: modalData.trainingClass.map((item,index)=>{
                        return {...item};
                    })
                });
            }
            yield put({
                type: 'updateState',
                payload: {
                    dataSource: d,
                    isShowModal: false
                }
            })
        },
        *deleteDevice({payload},{select,call,put}){
            let {dataSource} = yield select(({example})=>example);
            let {rowData} = payload;
            let d = dataSource.filter((item,index)=>{
                return (index+1) !== rowData.index; 
            })
            yield put({
                type: 'updateState',
                payload: {
                    dataSource: d
                }
            })
        },
        *deleteTeam({payload},{select,call,put}){
            let {modalData} = yield select(({example})=>example);
            let {rowData} = payload;
            let m = {...modalData};
            let cs = m.trainingClass.filter((item,index)=>{
                return item.index !== rowData.index;
            })
            yield put({
                type: 'updateState',
                payload: {
                    modalData:{
                        ...m,
                        trainingClass: cs
                    }
                }
            })
        }
    },
    reducers: {
        updateState(state,action){
            return {...state,...action.payload};
        },
        //展示新增
        showAdd(state,action){
            let  {modalType,isShowModal} = action.payload;
            return {...state,modalType,isShowModal,
                requiredFieldValidator:true,modalData:{
                index: '',
                name: '',
                age: '',
                address: '',
                trainingClass: []
            }};
        },
        showChangeModal(state,action){
            let {rowData} = action.payload;
            return {...state,modalType: '报班',isShowModal:true,
                modalData: {
                    ...rowData
                }
            };
        },
        addTrainingClass(state,action){
            let m = {...state.modalData};
            let cs = [...m.trainingClass];
            cs.push({
                key: (cs[cs.length-1]?cs[cs.length-1].key:0)+1,
                index: (cs[cs.length-1]?cs[cs.length-1].index:0)+1,
                school: '',
                teacher: '',
                subject: ''
            })
            return {...state,modalData:{
                ...m,
                trainingClass: cs
            }}
        },
        changeTeamData(state,action){
            let {val,key,i} = action.payload;
            let m = {...state.modalData};
            let cs = m.trainingClass.map((item,index)=>{
                if(item.index == i){
                    return {
                        ...item,
                        [key]: val
                    }
                }
                return item;
            })
            return {...state,modalData:{
                ...m,
                trainingClass: cs
            }}
        }
    },
}
